package com.limf.demo.util;

import java.util.concurrent.atomic.AtomicInteger;

public class Increment_thread {

	volatile static boolean flag = false;

	public static void main(String[] args) {
		AtomicInteger num = new AtomicInteger();

		Thread t1 = new Thread(() -> {
			try {
				do {
					if (!flag && num.intValue()<=100) {
						System.out.println("Thread1:" + num.incrementAndGet());
						flag = true;
						Thread.sleep(10);

					}
				} while (num.intValue() < 100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		Thread t2 = new Thread(() -> {
			try {
				do {
					if (flag && num.intValue()<=100) {
						System.out.println("Thread2:" + num.incrementAndGet());
						flag = false;
						Thread.sleep(10);
					}
				} while (num.intValue() < 100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		t1.start();
		t2.start();
	}

}
